package com.lx.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lx.dao.OrderDao_Old;
import com.lx.entity.Order_Old;
import com.lx.service.OrderService_Old;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl_Old implements OrderService_Old {
    @Autowired
    OrderDao_Old orderDao;
    @Override
    public List<Order_Old> getAllOrders() {
        return orderDao.selectAll();
    }

    @Override
    public PageInfo<Order_Old> getPageInfo(Map<String, Object> params) {
        int pageNum = 1;
        int pageSize = 5;
        if (params.containsKey("pageNum")) {
            pageNum = Integer.parseInt(params.get("pageNum").toString());
        }
        if (params.containsKey("pageSize")) {
            pageSize = Integer.parseInt(params.get("pageSize").toString());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Order_Old> orderList = orderDao.selectByPage(params);

        PageInfo<Order_Old> pageInfo = new PageInfo<>(orderList);
        return pageInfo;
    }

    @Override
    public Order_Old selectById(int id) {
        return orderDao.selectById(id);
    }

    @Override
    public boolean addOrder(Order_Old order, int productId) {
        try {
            // 通过productId获取商品信息
            Map<String, Object> product = orderDao.getProductById(productId);
            if (product == null) {
                throw new RuntimeException("指定的商品不存在");
            }

            // 设置商品信息
            order.setName((String) product.get("name"));
            order.setPrice(new BigDecimal(product.get("price").toString()) );
            order.setFileName((String) product.get("fileName"));
//            order.setQuantity(quantity);
//            order.setQuantity((Integer) order.get("quantity"));




            // 计算总价 = 商品单价 × 数量
            BigDecimal totalPrice = order.getPrice().multiply(new BigDecimal(order.getQuantity()));
            order.setUnitCost(totalPrice); // 订单详情总价
            order.setCost(totalPrice);     // 订单总价

            // 插入订单主表
            int result = orderDao.addOrder(order);
            if (result > 0) {
                int orderId = order.getId(); // 获取生成的订单ID

                // 插入订单详情表
                Map<String, Object> orderDetail = new HashMap<>();
                orderDetail.put("orderId", orderId);
                orderDetail.put("productId", productId);
                orderDetail.put("quantity", order.getQuantity());
                orderDetail.put("unitCost", order.getUnitCost()); // 订单详情总价

                orderDao.addOrderDetail(orderDetail);

                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }



    @Override
    public boolean updateOrder(Order_Old order) {
        return orderDao.updateOrder(order) > 0;
    }

    @Override
    public boolean delOrder(int id) {
        try {
            // 先删除订单详情
            orderDao.delOrderDetailByOrderId(id);
            // 再删除订单主表
            return orderDao.delOrder(id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
